home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacQForth 1.0 / asm6502 / asm6502 source / asm.c next >
Text File  |  1995-02-27  |  27KB  |  437 lines

  1. {
  2.     if (lflag > 0)
  3.         fprintf(stdout, "%s\n", prlnbuf);
  4. }
  5.  
  6. /* colsym() collects a symbol from prlnbuf into symbol0[],
  7.  *    leaves prlnbuf pointer at first invalid symbol character,
  8.  *    returns 0 if no symbol collected
  9.  */
  10.  
  11. colsym(ip)
  12.     int *ip;
  13. {
  14.     int    valid;
  15.     int    i;
  16.     char    ch;
  17.  
  18.     valid = 1;
  19.     i = 0;
  20.     while (valid == 1) {
  21.         ch = prlnbuf[*ip];
  22.         if (ch == '_' || ch == '.');
  23.         else if (ch >= 'a' && ch <= 'z');
  24.         else if (ch >= 'A' && ch <= 'Z');
  25.         else if (i >= 1 && ch >= '0' && ch <= '9');
  26.         else if (i == 1 && ch == '=');
  27.         else valid = 0;
  28.         if (valid == 1) {
  29.             if (i < SBOLSZ - 1)
  30.                 symbol0[++i] = ch;
  31.             (*ip)++;
  32.         }
  33.     }
  34.     if (i == 1) {
  35.         switch (symbol0[1]) {
  36.         case 'A': case 'a':
  37.         case 'X': case 'x':
  38.         case 'Y': case 'y':
  39.             error("Symbol is reserved (A, X or Y)");
  40.             i = 0;
  41.         }
  42.     }
  43.     symbol0[0] = i;
  44.     return(i);
  45. }
  46.  
  47. /* symbol table lookup
  48.  *    if found, return pointer to symbol
  49.  *    else, install symbol as undefined, and return pointer
  50.  */
  51.  
  52. stlook()
  53. {
  54.     int    found;
  55.     int    hptr;
  56.     int    j;
  57.     int    nptr;
  58.     int    pptr;
  59.     int    ptr;
  60.  
  61.     hptr = 0;
  62.     for (j = 0; j < symbol0[0]; j++)
  63.         hptr += symbol0[j];
  64.     hptr %= 128;
  65.     ptr = hash_tbl[hptr];
  66.     if (ptr == -1) {        /* no entry for this link */
  67.         hash_tbl[hptr] = nxt_free;
  68.         return(stinstal());
  69.     }
  70.     while (symtab[ptr] != 0) {    /* 0 count = end of table */
  71.         found = 1;
  72.         for (j = 0; j <= symbol0[0]; j++) {
  73.             if (symbol0[j] != symtab[ptr + j]) {
  74.                 found = 0;
  75.                 pptr = ptr + symtab[ptr] + 4;
  76.                 nptr = (symtab[pptr + 1] << 8) + (symtab[pptr] & 0xff);
  77.                 nptr &= 0xffff;
  78.                 if (nptr == 0) {
  79.                     symtab[ptr + symtab[ptr] + 4] = nxt_free & 0xff;
  80.                     symtab[ptr + symtab[ptr] + 5] = (nxt_free >> 8) & 0xff;
  81.                     return(stinstal());
  82.                 }
  83.                 ptr = nptr;
  84.                 break;
  85.             }
  86.         }
  87.         if (found == 1)
  88.             return(ptr);
  89.     }
  90.     error("Symbol not found");
  91.     return(-1);
  92. }
  93.  
  94. /*  instal symbol into symtab
  95.  */
  96. stinstal()
  97. {
  98. register    int    j;
  99. register    int    ptr1;
  100. register    int    ptr2;
  101.  
  102.     ptr1 = ptr2 = nxt_free;
  103.     if ((ptr1 + symbol0[0] + 6) >= STABSZ) {
  104.         error("Symbol table full");
  105.         return(-1);
  106.     }
  107.     for (j = 0; j <= symbol0[0]; j++)
  108.         symtab[ptr1++] = symbol0[j];
  109.     symtab[ptr1] = udtype;
  110.     nxt_free = ptr1 + 5;
  111.     return(ptr2);
  112. }
  113.  
  114. /* operation code table lookup
  115.  *    if found, return pointer to symbol,
  116.  *    else, return -1
  117.  */
  118.  
  119. oplook(ip)
  120.    int    *ip;
  121. {
  122. register    char    ch;
  123. register    int    i;
  124. register    int    j;
  125.     int    k;
  126.     int    temp[2];
  127.  
  128.     i = j = 0;
  129.     temp[0] = temp[1] = 0;
  130.     while((ch=prlnbuf[*ip])!= ' ' && ch!= 0 && ch!= '\t' && ch!= ';') {
  131.         if (ch >= 'A' && ch <= 'Z')
  132.             ch &= 0x1f;
  133.         else if (ch >= 'a' && ch <= 'z')
  134.             ch &= 0x1f;
  135.         else if (ch == '.')
  136.             ch = 31;
  137.         else if (ch == '*')
  138.             ch = 30;
  139.         else if (ch == '=')
  140.             ch = 29;
  141.         else return(-1);
  142.         temp[j] = (temp[j] * 0x20) + (ch & 0xff);
  143.         if (ch == 29)
  144.             break;
  145.         ++(*ip);
  146.         if (++i >= 3) {
  147.             i = 0;
  148.             if (++j >= 2) {
  149.                 return(-1);
  150.             }
  151.         }
  152.     }
  153.     if ((j = temp[0]^temp[1]) == 0)
  154.         return(-2);
  155.     k = 0;
  156.     i = step[k] - 3;
  157.     do {
  158.         if (j == optab[i]) {
  159.             opflg = optab[++i];
  160.             opval = optab[++i];
  161.             return(i);
  162.         }
  163.         else if (j < optab[i])
  164.             i -= step[++k];
  165.         else i += step[++k];
  166.     } while (step[k] != 0);
  167.     return(-1);
  168. }
  169.  
  170. /* error printing routine */
  171.  
  172. error(stptr)
  173.    char *stptr;
  174. {
  175.     loadlc(loccnt, 0, 1);
  176.     loccnt += 3;
  177.     loadv(0,0,0);
  178.     loadv(0,1,0);
  179.     loadv(0,2,0);
  180.     fprintf(stdout, "%s\n", prlnbuf);
  181.     fprintf(stdout, "%s\n", stptr);
  182.     errcnt++;
  183. }
  184.  
  185. /* load 16 bit value in printable form into prlnbuf */
  186.  
  187. loadlc(val, f, outflg)
  188.     int val;
  189.     int f;
  190.     int outflg;
  191. {
  192.     int    i;
  193.  
  194.     i = 6 + 7*f;
  195.     hexcon(4, val);
  196.     if (nflag == 0) {
  197.         prlnbuf[i++]  = hex[3];
  198.         prlnbuf[i++]  = hex[4];
  199.         prlnbuf[i++]  = ':';
  200.         prlnbuf[i++]  = hex[1];
  201.         prlnbuf[i] = hex[2];
  202.     }
  203.     else {
  204.         prlnbuf[i++] = hex[1];
  205.         prlnbuf[i++] = hex[2];
  206.         prlnbuf[i++] = hex[3];
  207.         prlnbuf[i] = hex[4];
  208.     }
  209.     if ((pass == LAST_PASS)&&(oflag != 0)&&(objcnt <= 0)&&(outflg != 0)) {
  210.         fprintf(optr, "\n;%c%c%c%c", hex[3], hex[4], hex[1], hex[2]);
  211.         objcnt = 16;
  212.     }
  213. }
  214.  
  215.  
  216.  
  217. /* load value in hex into prlnbuf[contents[i]] */
  218. /* and output hex characters to obuf if LAST_PASS & oflag == 1 */
  219.  
  220. loadv(val,f,outflg)
  221.    int    val;
  222.    int    f;        /* contents field subscript */
  223.    int    outflg;        /* flag to output object bytes */
  224. {
  225.  
  226.     hexcon(2, val);
  227.     prlnbuf[13 + 3*f] = hex[1];
  228.     prlnbuf[14 + 3*f] = hex[2];
  229.     if ((pass == LAST_PASS) && (oflag != 0) && (outflg != 0)) {
  230.         fputc(hex[1], optr);
  231.         fputc(hex[2], optr);
  232.         --objcnt;
  233.     }
  234. }
  235.  
  236. /* convert number supplied as argument to hexadecimal in hex[digit] (lsd)
  237.         through hex[1] (msd)        */
  238.  
  239. hexcon(digit, num)
  240.     int digit;
  241.    int    num;
  242. {
  243.  
  244.     for (; digit > 0; digit--) {
  245.         hex[digit] = (num & 0x0f) + '0';
  246.         if (hex[digit] > '9')
  247.             hex[digit] += 'A' -'9' - 1;
  248.         num >>= 4;
  249.     }
  250. }
  251.  
  252. /* assign <value> to label pointed to by lablptr,
  253.  *    checking for valid definition, etc.
  254.  */
  255.  
  256. labldef(lval)
  257.     int lval;
  258. {
  259.     int    i;
  260.  
  261.     if (lablptr != -1) {
  262.         lablptr += symtab[lablptr] + 1;
  263.         if (pass == FIRST_PASS) {
  264.             if (symtab[lablptr] == UNDEF) {
  265.                 symtab[lablptr + 1] = lval & 0xff;
  266.                 i = symtab[lablptr + 2] = (lval >> 8) & 0xff;
  267.                 if (i == 0)
  268.                     symtab[lablptr] = DEFZRO;
  269.                 else    symtab[lablptr] = DEFABS;
  270.             }
  271.             else if (symtab[lablptr] == UNDEFAB) {
  272.                 symtab[lablptr] = DEFABS;
  273.                 symtab[lablptr + 1] = lval & 0xff;
  274.                 symtab[lablptr + 2] = (lval >> 8) & 0xff;
  275.             }
  276.             else {
  277.                 symtab[lablptr] = MDEF;
  278.                 symtab[lablptr + 1] = 0;
  279.                 symtab[lablptr + 2] = 0;
  280.                 error("Label multiply defined");
  281.                 return(-1);
  282.             }
  283.         }
  284.         else {
  285.             i = (symtab[lablptr + 2] << 8) +
  286.                 (symtab[lablptr+1] & 0xff);
  287.             i &= 0xffff;
  288.             if (i != lval && pass == LAST_PASS) {
  289.                 error("Sync error");
  290.                 return(-1);
  291.             }
  292.         }
  293.     }
  294.     return(0);
  295. }
  296.  
  297. /* determine the value of the symbol,
  298.  * given pointer to first character of symbol in symtab
  299.  */
  300.  
  301. symval(ip)
  302.     int *ip;
  303. {
  304.     int    ptr;
  305.     int    svalue;
  306.  
  307.     svalue = 0;
  308.     colsym(ip);
  309.     if ((ptr = stlook()) == -1)
  310.         undef = 1;        /* no room error */
  311.     else if (symtab[ptr + symtab[ptr] + 1] == UNDEF)
  312.         undef = 1;
  313.     else if (symtab[ptr + symtab[ptr] + 1] == UNDEFAB)
  314.         undef = 1;
  315.     else svalue = ((symtab[ptr + symtab[ptr] + 3] << 8) +
  316.         (symtab[ptr + symtab[ptr] + 2] & 0xff)) & 0xffff;
  317.     if (symtab[ptr + symtab[ptr] + 1] == DEFABS)
  318.         zpref = 1;
  319.     if (undef != 0)
  320.         zpref = 1;
  321.     return(svalue);
  322. }
  323.  
  324.  
  325. /* class 1 machine operations processor - 1 byte, no operand field */
  326.  
  327. class1()
  328. {
  329.     if (pass == LAST_PASS) {
  330.         loadlc(loccnt, 0, 1);
  331.         loadv(opval, 0, 1);
  332.         println();
  333.     }
  334.     loccnt++;
  335. }
  336.  
  337.  
  338. /* class 2 machine operations processor - 2 byte, relative addressing */
  339.  
  340. class2(ip)
  341.     int *ip;
  342. {
  343.  
  344.     if (pass == LAST_PASS) {
  345.         loadlc(loccnt, 0, 1);
  346.         loadv(opval, 0, 1);
  347.         while (prlnbuf[++(*ip)] == ' ');
  348.         if (evaluate(ip) != 0) {
  349.             loccnt += 2;
  350.             return;
  351.         }
  352.         loccnt += 2;
  353.         if ((value -= loccnt) >= -128 && value < 128) {
  354.             loadv(value, 1, 1);
  355.             println();
  356.         }
  357.         else error("Invalid branch address");
  358.     }
  359.     else loccnt += 2;
  360. }
  361.  
  362.  
  363. /* class 3 machine operations processor - various addressing modes */
  364.  
  365. class3(ip)
  366.     int *ip;
  367. {
  368.     char    ch;
  369.     int    code;
  370.     int    flag;
  371.     int    i;
  372.     int    ztmask;
  373.  
  374.     while ((ch = prlnbuf[++(*ip)]) == ' ');
  375.     switch(ch) {
  376.     case 0:
  377.     case ';':
  378.         error("Operand field missing");
  379.         return;
  380.     case 'A':
  381.     case 'a':
  382.         if ((ch = prlnbuf[*ip + 1]) == ' ' || ch == 0) {
  383.             flag = ACC;
  384.             break;
  385.         }
  386.     default:
  387.         switch(ch = prlnbuf[*ip]) {
  388.         case '#': case '=':
  389.             flag = IMM1 | IMM2;
  390.             ++(*ip);
  391.             break;
  392.         case '(':
  393.             flag = IND | INDX | INDY;
  394.             ++(*ip);
  395.             break;
  396.         default:
  397.             flag = ABS | ZER | ZERX | ABSX | ABSY | ABSY2 | ZERY;
  398.         }
  399.         if ((flag & (INDX | INDY | ZER | ZERX | ZERY) & opflg) != 0)
  400.             udtype = UNDEFAB;
  401.         if (evaluate(ip) != 0)
  402.             return;
  403.         if (zpref != 0) {
  404.             flag &= (ABS | ABSX | ABSY | ABSY2 | IND | IMM1 | IMM2);
  405.             ztmask = 0;
  406.         }
  407.         else ztmask = ZER | ZERX | ZERY;
  408.         code = 0;
  409.         i = 0;
  410.         while (( ch = prlnbuf[(*ip)++]) != ' ' && ch != 0 && i++ < 4) {
  411.             code *= 8;
  412.             switch(ch) {
  413.             case ')':        /* ) = 4 */
  414.                 ++code;
  415.             case ',':        /* , = 3 */
  416.                 ++code;
  417.             case 'X':        /* X = 2 */
  418.             case 'x':
  419.                 ++code;
  420.             case 'Y':        /* Y = 1 */
  421.             case 'y':
  422.                 ++code;
  423.                 break;
  424.             default:
  425.                 flag = 0;
  426.             }
  427.         }
  428.         switch(code) {
  429.         case 0:        /* no termination characters */
  430.             flag &= (ABS | ZER | IMM1 | IMM2);
  431.             break;
  432.         case 4:        /* termination = ) */
  433.             flag &= IND;
  434.             break;
  435.         case 25:        /* termination = ,Y */
  436.             flag &